package com.cloud.mall.sms.service.impl;

import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.sms.dao.SmsCouponDao;
import com.cloud.mall.sms.dao.SmsCouponProductCategoryRelationDao;
import com.cloud.mall.sms.dao.SmsCouponProductRelationDao;
import com.cloud.mall.sms.dto.SmsCouponParam;
import com.cloud.mall.sms.dto.SmsCouponProductCategoryRelation;
import com.cloud.mall.sms.dto.SmsCouponProductRelation;
import com.cloud.mall.sms.mapper.SmsCouponMapper;
import com.cloud.mall.sms.mapper.SmsCouponProductCategoryRelationMapper;
import com.cloud.mall.sms.mapper.SmsCouponProductRelationMapper;
import com.cloud.mall.sms.model.SmsCoupon;
import com.cloud.mall.sms.model.SmsCouponProductCategoryRelationExample;
import com.cloud.mall.sms.model.SmsCouponProductRelationExample;
import com.cloud.mall.sms.service.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SmsCouponServiceImpl implements SmsCouponService {
    @Autowired
    private SmsCouponMapper couponMapper;
    @Autowired
    private SmsCouponProductRelationDao productRelationDao;
    @Autowired
    private SmsCouponProductCategoryRelationDao productCategoryRelationDao;
    @Autowired
    private SmsCouponProductRelationMapper productRelationMapper;
    @Autowired
    private SmsCouponProductCategoryRelationMapper productCategoryRelationMapper;
    @Autowired
    private SmsCouponDao couponDao;


    @Transactional
    @Override
    public BaseResponse create(SmsCouponParam couponParam) {
        couponParam.setCount(couponParam.getPublishCount());
        couponParam.setUseCount(0);
        couponParam.setReceiveCount(0);
        SmsCoupon smsCoupon = MallBeanUtils.dtoToDo(couponParam, SmsCoupon.class);
        //插入优惠券表
        int count = couponMapper.insert(smsCoupon);
        //插入优惠券和商品关系表
        if(couponParam.getUseType().equals(2)){
            for(SmsCouponProductRelation productRelation:couponParam.getProductRelationList()){
                productRelation.setCouponId(couponParam.getId());
            }
            productRelationDao.insertList(null);
        }
        //插入优惠券和商品分类关系表
        if(couponParam.getUseType().equals(1)){
            for (SmsCouponProductCategoryRelation couponProductCategoryRelation : couponParam.getProductCategoryRelationList()) {
                couponProductCategoryRelation.setCouponId(couponParam.getId());
            }
            productCategoryRelationDao.insertList(null);
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse update(Long id, SmsCouponParam couponParam) {
        couponParam.setId(id);
        SmsCoupon smsCoupon = MallBeanUtils.dtoToDo(couponParam, SmsCoupon.class);
        int count =couponMapper.updateByPrimaryKey(smsCoupon);
        //删除后插入优惠券和商品关系表
        return BaseResponse.setResultSuccess(count);
    }

    @Transactional
    @Override
    public BaseResponse delete(Long id) {
        //删除优惠券
        int count = couponMapper.deleteByPrimaryKey(id);
        //删除商品关联
        deleteProductRelation(id);
        //删除商品分类关联
        deleteProductCategoryRelation(id);
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse<SmsCouponParam> getItem(Long id) {
        SmsCouponParam smsCouponParam= couponDao.getItem(id);
        return BaseResponse.setResultSuccess(smsCouponParam);
    }

  /*  @Override
    public BaseResponse<CommonPage<SmsCoupon>> list(String name, Integer type, Integer pageSize, Integer pageNum) {
        SmsCouponExample example = new SmsCouponExample();
        SmsCouponExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(type!=null){
            criteria.andTypeEqualTo(type);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<SmsCoupon> couponList= couponMapper.selectByExample(example);
        return BaseResponse.setResultSuccess();
    }*/

    private void deleteProductRelation(Long id) {
        SmsCouponProductRelationExample productRelationExample = new SmsCouponProductRelationExample();
        productRelationExample.createCriteria().andCouponIdEqualTo(id);
        productRelationMapper.deleteByExample(productRelationExample);
    }

    private void deleteProductCategoryRelation(Long id) {
        SmsCouponProductCategoryRelationExample productCategoryRelationExample = new SmsCouponProductCategoryRelationExample();
        productCategoryRelationExample.createCriteria().andCouponIdEqualTo(id);
        productCategoryRelationMapper.deleteByExample(productCategoryRelationExample);
    }

}
